<?php include THEMES_PATH . 'public/before_body2.phtml'; ?>
<link href="/statics/front/css/cart.css" rel="stylesheet">
</head>
<body>
<?php include THEMES_PATH . 'public/header2.phtml';?>
<div class="main-width heading">
  <strong><?php echo L('_ORDER_INFORMATION_');?></strong>
</div>
<form action="<?php echo url('cart/checkout');?>" method="post" name="checkoutForm" id="checkoutForm">
  <input type="hidden" name="is_new_address" value="1" />
  <input type="hidden" name="ordertoken" value="<?php echo isset($ordertoken) ? $ordertoken : 0?>" />
  <input type="hidden" name="goods_amount" id="goods_amount" value="<?php echo isset($cartInfo['total_amount']) ? $cartInfo['total_amount'] : '0'; ?>" />
  <input type="hidden" name="goods_number" id="goods_number" value="<?php echo isset($cartInfo['total_number']) ? $cartInfo['total_number'] : '0'; ?>" />
  <input type="hidden" name="goods_weight" id="goods_weight" value="<?php echo isset($cartInfo['total_weight']) ? $cartInfo['total_weight'] : '0'; ?>" />
  <input type="hidden" name="fromcart" value="<?php echo isset($fromcart) ? $fromcart : 0?>" />
  <input type="hidden" name="ordertoken" value="<?php echo isset($ordertoken) ? $ordertoken : 0?>" />
  <input type="hidden" name="delivery_cid" id="delivery_cid" value="1" />
  <input type="hidden" name="delivery_pid" id="delivery_pid" />
  <input name="shipping_fee" id="input_shipping_money" type="hidden" value="0" />
  <input name="payment_fee" id="input_payment_money" type="hidden" value="0" />
  <input type="hidden" name="surplus_money" id="input_surplus_money" value="0" />
  <input type="hidden" name="integral_money" id="input_integral_money" value="0" />
  <input type="hidden" name="hongbao_money" id="input_hongbao_money" value="0" />
  <input type="hidden" name="hongbao_id" id="hongbao_id" value="0" />
  <input type="hidden" name="coupon_money" id="coupon_code_money" value="0" />
  <input type="hidden" name="coupon_code" id="coupon_code" value="0" />

  <div class="main-width clear">
    <div class="col-md-8 row col-wider">
      <dl class="address-fields col-md-12 mb2x">
        <dt class="caption"><i class="serial-num">1</i><?php echo L('delivery_address')?></dt>

        <dd class="col-md-12">
          <label><?php echo L('fristname')?>: <input type="text" name="delivery_first_name" required id="delivery_first_name" value="<?php echo isset($orderInfo['delivery_first_name']) ? $orderInfo['delivery_first_name'] : '';?>" /></label>
        </dd>
        <dd class="col-md-12">
          <label><?php echo L('state')?>: <input type="text" name="delivery_province" required id="delivery_province" value="<?php echo isset($orderInfo['delivery_province']) ? $orderInfo['delivery_province'] : '';?>" /></label>
        </dd>
        <dd class="col-md-12">
          <label><?php echo L('city')?>: <input type="text" name="delivery_city" required id="delivery_city" value="<?php echo isset($orderInfo['delivery_city']) ? $orderInfo['delivery_city'] : '';?>" /></label>
        </dd>
        <dd class="col-md-12">
          <label><?php echo L('_STREET_LINE1_');?>: <input type="text" name="delivery_address" required id="delivery_address" value="<?php echo isset($orderInfo['delivery_address']) ? $orderInfo['delivery_address'] : '';?>" /></label>
        </dd>
        <dd class="col-md-12">
          <label><?php echo L('_STREET_LINE2_');?>: <input type="text" name="delivery_address2" required id="delivery_address2" value="<?php echo isset($orderInfo['delivery_address2']) ? $orderInfo['delivery_address2'] : '';?>" /></label>
        </dd>
        <dd class="col-md-12">
          <label><?php echo L('phone')?>: <input type="text" name="delivery_phone" id="delivery_phone" value="<?php echo isset($orderInfo['delivery_phone']) ? $orderInfo['delivery_phone'] : '';?>" required></label>
        </dd>
        <dd class="col-md-12">
          <label><?php echo L('email')?>: <input type="text" name="delivery_email" required id="delivery_email" value="<?php echo isset($orderInfo['delivery_email']) ? $orderInfo['delivery_email'] : '';?>"/></label>
        </dd>
        <dd class="check-box col-md-12 ">
          <label><input type="checkbox" name="join_our_mailing" checked /><?php echo L('join_our_mailing');?></label>
        </dd>
      </dl>
      <dl class="definition-list shipping-method js-shipping-method">
        <dt class="caption"><input type="hidden" name="shipping_method" value="<?php foreach($shippingList as $shipping) : if ($shipping['status']){echo $shipping['name'];break;};endforeach;?>" /><i>2</i><?php echo L('shipping_method')?></dt>
        <?php
         if(isset($shippingList) && $shippingList):
            $i=0;
            foreach ($shippingList as $key=>$shipping):
             if (!$shipping['status']) continue;
                $i++;
        ?>
        <dd<?php if ($key==0){?> class="selected"<?php } ?> data-info="<?php echo $shipping['name'];?>">
          <p class="text"><?php echo isset($shipping['title']) ? $shipping['title'] : null;?></p>
          <p class="align-center title"><?php if ($key==0){?><?php echo L('_STANDARD_');?><?php }else{?><?php echo L('_EXPEDITED_');?><?php }?></p>
          <p><?php echo $shippingList[$key]['description'];?></p>
          <i class="mark"></i>
        </dd>
        <?php
            endforeach;
         endif;
        ?>
      </dl>
      <dl class="definition-list payment-method js-payment-method">
        <dt class="caption">
          <input type="hidden" name="payment_method" value="<?php foreach($paymentList as $payment) : if ($payment['status']){echo $payment['name'];break;};endforeach;?>">
          <i>3</i>
          <?php echo L('payment_method')?>
        </dt>
        <?php
          if(isset($paymentList) && $paymentList):
          $i=1;
          foreach ($paymentList as $k => $payment) :
            if (!$payment['status']) continue;
        ?>
        <dd<?php if ($i==1){?> class="selected"<?php } ?> data-info="<?php echo $payment['name'];?>">
          <p class="text"><?php if($payment['name'] =="Visabill"){echo L('_credit_');}elseif($payment['name'] == "Paypal"){echo L('_paypal_');}elseif($payment['name'] == "Worldpay"){echo L('_worldpay_');};?></p>
          <img src="/statics/front/images/cart/<?php echo strtolower($payment['name']);?>.png" alt="<?php echo isset($payment['name']) ? $payment['name'] : null ;?>">
          <i class="mark"></i>
        </dd>
        <?php
             $i++;
            endforeach;
          endif;
        ?>
      </dl>
      <div class="caption" style="margin-bottom:2em;"><i class="serial-num">4</i><?php echo L('order_summary')?></div>
      <table class="align-center shopping-list">
        <tr>
          <th width="10%"><?php echo L('goods_img')?></th>
          <th width="40%" align="left"></th>
          <th width="10%"><?php echo L('goods_price')?></th>
          <th width="10%"><?php echo L('goods_number')?></th>
          <th width="20%"><?php echo L('amount_about')?></th>
          <th width="15%"><?php echo L('figure')?></th>
        </tr>
        <?php
          if ($cartInfo) :
          foreach ($cartInfo['productList'] as $k => $cart) :
          $product = $cart['product'];
        ?>
        <tr>
          <td>
            <a href="<?php echo url('product/'.$product['id']).'-'.generateUrl_un($product['english_name']);?>"><img src="<?php echo $product['small_img'] ?>" /></a>
          </td>
          <?php
            $advanced_price = $product['advanced_price'];
            $original_price = $product['original_price'];
          ?>
          <td align="left">
            <a href="<?php echo url('product/'.$product['id']).'-'.generateUrl_un($product['english_name']);?>" class="link-gray"><?php echo isset($product['name']) ? $product['name'] : null ?></a>
          </td>
          <td>
            <!--游客购买原价格显示-->
            <?php
            if($original_price > 0){
              $price = currency($product['original_price']);
              $new_price="<del>$price</del>";
              echo $new_price;}
              ?>
            <?php echo isset($product['price']) ? currency($product['price']) : null ?>
          </td>
          <td><?php echo isset($cart['qty']) ? $cart['qty'] : null ?></td>
          <td>
            <span id="goods_amount_<?php echo $cart['id']; ?>"><?php echo currency($product['goods_amount']) ?></span>
          </td>
          <td>
            <?php
              $measureInfo = AttributeModel::get_user_measure_info($cart['measure_id']);
                if($measureInfo['m_status'] == 5):
            ?>
            <p>
            <?php foreach(unserialize($measureInfo['measure_data']) as $mskey=>$msval):?>
            <?php echo MeasureModel::msizedataShow($mskey);?>:<strong class="text-bold"><?php echo $msval;?></strong><br>
            <?php endforeach;?>
            </p>
            <?php else:?>
            <?php echo $measureInfo['measure_name'];?>
            <?php endif;?>
          </td>
        </tr>
        <?php if($cart['pants_qty'] > 0):
          $add_pants = $cart['add_pants'];
        ?>
        <tr>
          <td></td>
          <td align="left"><?php echo L('AdditionalPants');?></td>
          <td><?php echo isset($add_pants['price']) ? currency($add_pants['price']) : null ?></td>
          <td><?php echo isset($cart['pants_qty']) ? $cart['pants_qty'] : null ?></td>
          <td><span id="goods_amount_<?php echo $cart['id']; ?>_pants"><?php echo currency($add_pants['price'] * $cart['pants_qty']);?></span></td>
          <td></td>
        </tr>
        <?php endif;?>
        <?php
            endforeach;
          endif;
        ?>
      </table>
      <p class="align-right" style="margin:1em 0 5em;">
        <a href="<?php echo url('cart/info');?>"><?php echo L('_back_cart_')?></a>
      </p>
      <dl class="definition-list message-box">
        <dt class="caption"><i>5</i><?php echo L('_order_msg_')?></dt>
        <dd class="textarea-box"><textarea name="remark"></textarea></dd>
      </dl>
      <p class="align-center">
        <input type="submit" name="dosubmit" value="<?php echo L('_PLACE_ORDER_');?>" class="btn btn-middle place-order js-place-order">
      </p>
    </div>
    <div class="col-md-4 bg-sliver">
      <div class="sidebar">
        <ul class="order-details">
          <li>
            <em><?php echo L('goods_amount_total')?>:</em>
            <strong id="cart_goods_amount"><?php echo currency($cartInfo['total_amount']) ?></strong>
          </li>
          <li>
            <i>+</i>
            <em><?php echo L('shipping_fee')?>:</em>
            <strong id="shipping_money">0</strong>
          </li>
          <?php if($plus_size_Fee){?>
          <li>
            <i>+</i>
            <em>附加费:</em>
            <strong id="surcharge"><?php echo currency($plus_size_Fee);?></strong>
          </li>
          <?php }?>
          <li>
            <i>-</i>
            <em><?php echo L('order_discount_you_save')?>:</em>
            <strong id="discount_money">0</strong>
          </li>
          <li class="order-amount">
            <em><?php echo L('order_to_pay_amount');?>:</em>
            <strong id="order_amount"><?php echo isset($cartInfo['total_amount']) ? currency($cartInfo['total_amount']) : null;?></strong>
          </li>
        </ul>
        <?php if(!$joinShirtCleanr):?>
        <div class="coupon-box js-coupon-box">
          <label>
            <input type="checkbox" autocomplete="off" class="vm">
            <em class="normal vm"><?php echo L('coupon_code');?></em>
          </label>
          <input type="text" name="favorable" id="favorable">
          <button type="button"><?php echo L('submit');?></button>
        </div>
        <?php endif; ?>
        <input type="submit" name="dosubmit" value="<?php echo L('_PLACE_ORDER_');?>" class="width-full mt1x mb1x btn btn-middle place-order js-place-order">
        <ul class="align-center shopping-clause">
          <li>
            <span><?php echo L('receiver_by_november');?></span>
            <p><img src="/statics/front/images/icons/lightning.png" alt="<?php echo L('receiver_by_november');?>"></p>
          </li>
          <li>
            <span><?php echo L('free_shipping');?></span>
            <p><img src="/statics/front/images/icons/shipment.png" alt="<?php echo L('free_shipping');?>"></p>
          </li>
          <li>
            <span><?php echo L('fit_promise');?></span>
            <p><img src="/statics/front/images/icons/fit.png" alt="<?php echo L('fit_promise');?>"></p>
          </li>
        </ul>
      </div>
    </div>
  </div>
</form>

<script src="/statics/front/js/scripts/cart.js"></script>
<script>
$(function() {
  function changefavorable(ajaxDone) {
    var is_user_money = $('#is_user_money:checked').val();
    var shipping_free = $('#input_shipping_money').val();
    var goods_amount = $('#goods_amount').val();
    var payment_fee = $('#input_payment_money').val();
    var integral_money = $('#input_integral_money').val();
    var hongbao_money = $('#input_hongbao_money').val();
    var coupon_money = $('#coupon_code_money').val();
    var use_integral = $('#use_integral').val();
    var favorable = $('#order_amount').text();
    var favorableCode = $('#favorable').val();

    if (isNaN(use_integral)) {
      $('#use_integral').val(0);
      use_integral = 0;
    }
    var amount = parseFloat(parseFloat(goods_amount) + parseFloat(payment_fee) + parseFloat(shipping_free) - parseFloat(hongbao_money));
    if (favorable) {
      $.get(url('cart/favorable/amount/' + amount + '/favorables/' + favorableCode), function(res) {
        var res = eval('(' + res + ')');

        if (res['error'] == 1) {
          var price = res['price'];
          paymentc($('#discount_money'), price);
          $('#coupon_code_money').val(price);
          $('#coupon_code').val(favorableCode);
          getSurplusMoney();
        } else {
          paymentc($('#discount_money'), 0);
          $('#coupon_code_money').val(0);
          $('#coupon_code').val('');
          getSurplusMoney();
          alert(res['errStr']);
        }

        if (typeof ajaxDone === 'function') {
          ajaxDone();
        }
      });
    } else {
      paymentc($('#discount_money'), 0);
      $('#coupon_code_money').val(0);
      $('#coupon_code').val('');
      getSurplusMoney();
    }
  }

  function getAllCost() {//计算各项费用的总和
    var $goodsAmount = $('#cart_goods_amount'), //商品金额
        $freight = $('#shipping_money'),        //运费
        $surcharge = $('#surcharge'),           //（超重）附加费
        $costSaving = $('#discount_money'),     //折扣费
        digitReg = /[0-9]+(\.{1}[0-9]+|\.{0}[0-9]{0})/,
        goodsAmount = (goodsAmount = digitReg.exec($goodsAmount.text())) && goodsAmount[0],
        freight = (freight = digitReg.exec($freight.text())) && freight[0],
        surcharge = (surcharge = digitReg.exec($surcharge.text())) && surcharge[0],
        costSaving = (costSaving = digitReg.exec($costSaving.text())) && costSaving[0];

    return Number(goodsAmount) + Number(freight) + Number(surcharge) - Number(costSaving);
  }

  var $checkoutForm = $('#checkoutForm'),
      $orderAmount = $('#order_amount'),
      submitted = false;

  //自动触发
  <?php if(!empty($self_coupon)):?>
  $('.js-coupon-box').find('input[type=checkbox]').click();
  $('#favorable').val('<?php echo $self_coupon;?>');
  changefavorable(function () {
    $orderAmount.text('￥' + getAllCost());
  });
  <?php endif;?>

  $('#select_countries').on('change', function() { //选择国家
    var $this = $(this);
    if ($this.children('option:selected').val() == 9999) {
      $this.get(0).selectedIndex = ($(this).get(0).selectedIndex - 1);
    }
    console.log($this.val());
    $this.prev().text($this.children('option:selected').text()).parent().removeClass('error');
    $this.next().removeClass('show');
    getshippingmoney();
  });

  $checkoutForm.validate({ //地址验证
    errorElement: 'p',
    submitHandler: function(form) {
      $checkoutForm.find('.js-place-order').on('click', function() {
        $(this).addClass('disabled');
        if ($(this).hasClass('disabled')) {
          submitted = true;
        }
      });

      if (!submitted) {
        form.submit();
      }
    },
    errorPlacement: function(error, element) {
      element.after(error).parent().addClass('error');
    },
    success: function(label) {
      label.parent().removeClass('error');
    },
    rules: {
      delivery_cid:{
        required: true,
        min:1
      },
      delivery_phone: {
        required: true
      },
      delivery_email: {
        required: true,
        email: true
      }
    },
    messages: {
      delivery_email: {
        requried: $checkoutForm.find('input[name=delivery_email]').attr('title'),
        email: $checkoutForm.find('input[name=delivery_email]').data('email')
      }
    }
  });

  $('.js-shipping-method').find('dd').on('click', function() { //选择发货方式
    var $this = $(this);
    $this.parent().find('dt > input[type=hidden]').val($this.data('info'));
    $this.addClass('selected').siblings().removeClass('selected');
    getshippingmoney();
  });

  $('.js-payment-method').find('dd').on('click', function() { //选择支付方式
    var $this = $(this);
    $this.parent().find('dt > input[type=hidden]').val($this.data('info'));
    $this.addClass('selected').siblings().removeClass('selected');
    getPaymentMoney();
  });

  $('#favorable').next('button').on('click', function() {//使用优惠码
    changefavorable(function () {
      $orderAmount.text('￥' + getAllCost());
    });
  });

  $orderAmount.text('￥' + getAllCost());
});
</script>
<?php include THEMES_PATH.'public/footer2.phtml';?>
<?php include THEMES_PATH.'public/after_body.phtml';?>